package com.saifan.wyy_ov.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.flyco.labelview.LabelView;
import com.malinskiy.superrecyclerview.OnMoreListener;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.common.NetActivity;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.data.bean.NoticeBean;
import com.saifan.wyy_ov.utils.TimeUtil;
import com.saifan.wyy_ov.utils.TimeUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lomasky.ma.httpUtils.JsonUtil;
import lomasky.ma.httpUtils.ResponseBean;
import lomasky.ma.xui.ArrayView;
import lomasky.ma.xui.BaseRecyclerViewAdapter;
import lomasky.ma.xui.BaseViewHolder;
import lomasky.ma.xui.ItemDivider;
import lomasky.ma.xui.RecyclerViewAdapter;

/**
 * @author loma
 * @Description:
 * @date: 2016/7/27 0027
 * @version: V2.0
 */
public class NoticeListActivity extends NetActivity {

    private lomasky.ma.xui.ArrayView arrayView;
    public int page = 1;
    private List<NoticeBean> noticeBeans;
    private RecyclerViewAdapter<NoticeBean> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.arrayview);
        setEnabledNavigation(true);
        noticeBeans = (List<NoticeBean>) getIntent().getSerializableExtra("NoticeBean");
        if (noticeBeans.isEmpty()) {
            page = 0;
        }
        this.arrayView = (ArrayView) findViewById(R.id.arrayView);
        arrayView.addItemDecoration(new ItemDivider(this,R.color.divide_color));
        arrayView.setupMoreListener(new OnMoreListener() {
            @Override
            public void onMoreAsked(int i, int i1, int i2) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        arrayView.getMoreProgressView().setVisibility(View.GONE);
                        query();
                    }
                }, 500);

            }
        });
        adapter = new RecyclerViewAdapter<NoticeBean>(this, R.layout.home_notify_item) {
            @Override
            public void onBind(BaseViewHolder holder, NoticeBean noticeBean, int i) {
                holder.setText(R.id.GG_bt, noticeBean.getGG_bt());
                holder.setText(R.id.GG_LR, noticeBean.getGG_LR());
                holder.setText(R.id.GG_CJSJ, TimeUtils.getTime(noticeBean.getGG_CJSJ()));
                holder.setUrl(R.id.GG_TPLJ,Api.getUrlIp() +noticeBean.getGG_TPLJ());
                LabelView labelView = holder.getView(R.id.labelView);
                if (noticeBean.isGG_ZYX()) {

                    labelView.setBgColor(ContextCompat.getColor(context, R.color.material_lightBlue_700));

                    labelView.setText("重要");
                } else {

                    labelView.setBgColor(ContextCompat.getColor(context, R.color.alpha));
                    labelView.setText("");
                }

            }
        };
        adapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int i) {
                Intent intent  =new Intent(getBaseContext(),NotificationActivity.class);
                intent.putExtra("GG_ZJ", noticeBeans.get(i).getGG_ZJ());
                startActivity(intent);
            }
        });
        adapter.setData(noticeBeans);
        arrayView.setAdapter(adapter);

    }

    private void query() {
        Map<String, Object> params = new HashMap<>();
        params.put("XMBS", getRoomInfoBean().XMBS);
        params.put("Page", page);
        post(Api.Customer.NOTICE, params);
    }


    @Override
    public void err(String e) {

    }

    @Override
    public void ok(ResponseBean response) {

        List<NoticeBean> list = JsonUtil.getList(response.content, NoticeBean.class);
        if (!list.isEmpty()) {
            page++;
            adapter.addAll(list);

        } else {
            page--;
        }


    }
}
